<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Volume extends CI_Controller {
    
    private $data = array();

    public function  __construct() {
        parent::__construct();

        //check session
        if( !$this->session->userdata('isLoggedIn') ) {
            redirect(base_url().'admin', 'refresh');
        }

        //Load Helper and library
        $this->load->helper("url");
        $this->load->library("ci_smarty");
        $this->load->helper("text"); 
        $this->data['base_url'] = base_url();
        $this->data['root_url'] = $this->config->item('root_url');
        $this->load->library('pagination'); 

        //Load Model
        $this->load->Model("admin/Mvolume");
        $this->load->Model("admin/Mbook");

        $this->data['nameAdmin'] = $this->session->userdata('name');
    }

    public function listVolume($curPage = 1)
    {
        // load book
        $this->data['listBook'] = $this->Mbook->getBook(); 
        
        // config page
        $volumePageConfig['base_url']      = base_url("list-volume/");
        $volumePageConfig['total_rows']    = $this->Mvolume->countVolume();
        $volumePageConfig['per_page']      = 30;
        $volumePageConfig['uri_segment']   = 2;
        $volumePageConfig['cur_page']      = $curPage;

        $curOffset = $volumePageConfig['per_page'] * ($curPage - 1);

        $this->pagination->initialize($volumePageConfig);

        $this->data['pageList'] = $this->pagination->create_links();

        $this->data['listVolume'] = $this->Mvolume->getVolumeByLimit($volumePageConfig['per_page'],$curOffset);

        $this->data['sub_selected_cate'] = 'list-volume';
        
        $this->ci_smarty->view('backend/list-volume.tpl',$this->data); 
    }

    public function addVolume()
    {
        // load book
        $this->data['listBook'] = $this->Mbook->getBook(); 

        if($this->input->post()){
            $data = $this->input->post();
            $this->Mvolume->insertVolume($data); 
            redirect(base_url()."admin/list-volume.html"); 
        }

        $this->data['sub_selected_cate'] = 'add-volume';
        
        //$this->ci_smarty->view('backend/add-volume.tpl',$this->data); 
    }

    public function updateVolume($volume_id)
    {
        // load book
        $this->data['listBook'] = $this->Mbook->getBook(); 

        $this->data['volume'] = $this->Mvolume->getVolumeByID($volume_id); 

        if($this->input->post()){
            $data = $this->input->post();
            $this->Mvolume->updateVolume($data); 
            redirect(base_url()."admin/list-volume.html"); 
        }
       
        $this->ci_smarty->view('backend/update-volume.tpl',$this->data); 
    }

    public function deleteVolume($volume_id)
    {
        $this->Mvolume->deteleVolume($volume_id); 
        redirect(base_url()."admin/list-volume.html"); 
    }
}